let addBtn = document.querySelector('.add')
let delBtn = document.querySelector('.del')
let sortBtn = document.querySelector('.sort')
let allbtn = document.querySelector('.allSelect')
let rev = document.querySelector('.reverse')
let table = document.querySelector('table')
let tbody = document.querySelector('tbody')
let now = 0


addBtn.addEventListener('click', function() {
    let tr = document.createElement('tr');
    //补0
    tr.innerHTML = `<td>${String(now+1).padStart(2,'0')}</td>  
                    <td>${info[now%10]['name']}</td>
                    <td>${info[now%10]['age']}</td>
                    <td><button>删除</button></td>
                    <td> <input type="checkbox"></td>`;


    now++

    tbody.appendChild(tr)

})

//删除节点
tbody.addEventListener('click', function(e) {
    if (e.target.nodeName == 'BUTTON') {
        //e.target.parentElement.parentElement.remove()
        //.parentNode.parentNode 一样的效果
        tbody.removeChild(e.target.parentElement.parentElement)
    }
})

delBtn.addEventListener('click', function() {
        let inp = document.querySelectorAll('input:checked')
        inp.forEach(function(item) {
            item.parentElement.parentElement.remove()
            console.log(item);
        })
    })
    //排序功能
sortBtn.addEventListener('click', function() {
    //将第一行排除在外的所有tr标签
    let tr = document.querySelectorAll('tr:not(:first-child)')

    let trArr = []
        //将nodelist转换为array
    tr.forEach(function(item) {
        trArr.push(item)
    })

    //使用sort方法排序 根据年龄
    trArr.sort(function(a, b) {
        return a.children[2].innerHTML - b.children[2].innerHTML
    })

    trArr.forEach(function(item) {
        tbody.appendChild(item)
    })
})

//全选功能
allbtn.addEventListener('click', function() {
    let inp1 = document.querySelectorAll('input')
    for (let i = 0; i < inp1.length; i++) {
        inp1[i].checked = 'checked'
    }
})

//反选功能

rev.addEventListener('click', function() {
    let inp2 = document.querySelectorAll('input')
    for (let j = 0; j < inp2.length; j++) {
        inp2[j].checked = !inp2[j].checked
    }
})